<?php
	
		function db_connect($sql,$loca='localhost',$user='root',$pass='root',$utf='utf8'){


	
			$link = mysqli_connect($loca,$user,$pass,$sql);
			
			/*判断是否连接数据库*/
			if (!$link) {
				/*输出弹框数据库连接错误，返回上一页面*/
				echo '<script>alert("数据库连接错误");history.go(-1);</script>';die;
			}
			/*执行传输语句编码*/
			mysqli_query($link,'set names '.$utf);

			return $link;
			
		}

	
		/*删除*/

		function dei($table,$where){
			$str='';
			
			if (is_array($where)) {
				$str=implode(',',$where);
				$where="in ($str)";
				/*print_r($where);die;*/

			}else{

				$where ='= '.$where;
			}


			$sql="delete from $table  where `id` $where";
		
			mysqli_query($GLOBALS['link'],$sql);

	        return mysqli_affected_rows($GLOBALS['link']);
   
		};

		/*添加*/
		function add($table,$where){

			foreach ($where as $k => $v) {
				
				
					 $v="'".implode("','",array_values($where))."'";
					 $k="`".implode("`,`",array_keys($where))."`";
				
					
			
				
			}

			$sql = "insert into $table ($k)values($v)";

			mysqli_query($GLOBALS['link'],$sql);
	        $row = mysqli_affected_rows($GLOBALS['link']);
	       /* echo $row;die;*/
	        return  $row;
		}

		/*编辑*/
		function edit($table,$where,$id=1){
			
				
			$wher='';
			
			foreach ($where as $k => $v) {
				
				$i=0;

				/*print_r(count($where));die;*/

				if ($i==count($where)-1) {

					$wher.="`$k`='$v' ";
				

				}else{

					$wher.="`$k`='$v' ,";
				}
				$i++;

				$str='';
			}

			if (is_array($id)) {
				$str=implode(',',$id);
				$id="in ($str)";
				/*print_r($where);die;*/

			}else{

				$id = '= '.$id;
			}

				
			
			
			$wher=substr($wher,0,-1);


			$sql="update $table set $wher where `id` $id";

	        mysqli_query($GLOBALS['link'],$sql);

	       $row = mysqli_affected_rows($GLOBALS['link']);
	       return $row;
			}

		/*查询一次*/
		function getselectByone($table,$where=1,$filte='*'){
 
		   if (is_array($where)) {

			foreach ($where as $k => $v) {

				$i=0;
				/*print_r(count($where));die;*/
				if ($i==count($where)-1) {
					$where.="`$k`='$v'";
				

				}else{

					$where="`$k`='$v' and ";
				}
				$i++;
			}
		   }else{
		   		$where=$where;
		   }

			
			
		    $sql = "select $filte from $table where $where";
			$reseul=mysqli_query($GLOBALS['link'],$sql);
			$arr=mysqli_fetch_assoc($reseul);
		// print_r("select $filte from $table where $where");die;
			 
			return $arr;
		}
	
		
	/*查询多条*/

	function gatselect($table,$where=1,$limit='',$order='',$filte='*'){
		$where1='';
		
		if (!empty($order)) {
			$where1.='order by '.$order;
		};
		if (!empty($limit)) {
			
			$where1.=' limit '."$limit ";

		};

		$sql="select $filte from $table where $where $where1";
		
		 $result=mysqli_query($GLOBALS['link'],$sql);
		
		while ($arr=mysqli_fetch_assoc($result)) {
			 $data[]=$arr;
		}
		return $data;
	}
	

?>